package com.wildlife.springboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wildlife.springboot.common.Constants;
import com.wildlife.springboot.controller.dto.ConsumerDTO;
import com.wildlife.springboot.controller.dto.UserDTO;
import com.wildlife.springboot.entity.Consumer;
import com.wildlife.springboot.entity.User;
import com.wildlife.springboot.exception.ServiceException;
import com.wildlife.springboot.mapper.ConsumerMapper;
import com.wildlife.springboot.service.IConsumerService;
import org.springframework.stereotype.Service;

@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements IConsumerService {

    private static final Log LOG = Log.get();

    @Override
    public boolean login(ConsumerDTO consumerDTO) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", consumerDTO.getUsername());
        queryWrapper.eq("password", consumerDTO.getPassword());
        Consumer one = getOne(queryWrapper);
        return one != null;
    }

//    @Override
//    public Consumer register(ConsumerDTO consumerDTO) {
//        Consumer one = getConsumerInfo(consumerDTO);
//        if (one == null) {
//            one = new Consumer();
//            BeanUtil.copyProperties(consumerDTO, one, true);
//            save(one);      // 把 copy后的用户对象存储到数据库
//        } else {
//            throw new ServiceException(Constants.CODE_600, "用户已存在");
//        }
//        return one;
//    }
//
//    private Consumer getConsumerInfo(ConsumerDTO consumerDTO) {
//        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username", consumerDTO.getUsername());
//        queryWrapper.eq("password", consumerDTO.getPassword());
//        Consumer one;
//        try {
//            one = getOne(queryWrapper);
//        } catch (Exception e){
//            LOG.error(e);
//            throw new ServiceException(Constants.CODE_500, "系统错误");
//        }
//        return one;
//    }
}
